*Code to reproduce tables and figures from 'Measuring Congressional Partisanship and Its Consequences'

*Congressional Term Analysis
*use MeasuringCongressionalPartisanship_LSQ_CongressTermlData.dta"
*Descriptive statistics
sum partisan_total if omit==0
sum partisantweets if omit==0
*Total tweets 
collapse (sum) totaltweets if omit==0
*Negative tweets descriptives (footnote 10)
sum neg_part if omit==0
corr partisan_total neg_part_total

*Figure 1: Box and Whiskers Plot
graph hbox partisan_total if omit==0, over(congress, relabel(1 "114th Congress" 2 "115th Congress") label(labcolor(black))) ytitle("Proportion of Partisan Tweets") 

*Figure 2: Labeled Dot Plot
gen mc_label = "Nancy Pelosi" if icpsr==15448 
replace mc_label = "Mitch McConnell" if icpsr==14921 
replace mc_label = "Susan Collins" if icpsr==49703 
replace mc_label = "Joe Manchin" if icpsr==40915 
gen pos=3
*Dotplot was completed in Stata graph editor
dotplot partisan_total if omit==0, over(congress) mlabel(mc_label) mlabv(pos) ytitle(Partisan Intensity) title() xtitle(Congress) center nx(50)

***Validation Measures
*T-test comparing party leaders to rank-and-file
ttest partisan_total if omit==0, by(party_leader)
*Leadership includes committee chairs and ranking members
ttest partisan_total if omit==0, by(leadership)

*Table 5: Ideology and partisan intensity correlations
corr partisan_total ideologue if omit==0
corr partisan_total ideologue if senate==0 & omit==0 
corr partisan_total ideologue if senate==1 & omit==0 
corr partisan_total ideologue if party_code==200 & omit==0
corr partisan_total ideologue if party_code!=200 & omit==0
corr partisan_total ideologue if congress==114 & omit==0 
corr partisan_total ideologue if congress==115 & omit==0 

*Figure 3: Partian intensity and ideology scatter plots
label define republican 0 "Democrats"  1 "Republicans"
label values republican republican
twoway scatter partisan_total ideologue if omit==0, by(republican, note("")) xtitle("Ideological Extremity") ytitle("Partisan Intensity") 

*Figure 4: Scatter plot of partisan intensity and bipartisanship score
*NOTE: Reproducing this graph requires clearing the congressional term code and loading the annual data code to produce the party unity graph
twoway (scatter partisan_total lugar) if omit==0, xtitle(Bipartisanship Score) ytitle(Partisan Intensity)
*Save graph and title it "Bipartisan_Score.gph"
*This line of code comes from the annual data file
twoway (scatter partisan_total party_u if omit==0), xtitle(Party Unity Score) ytitle(Partisan Intensity)
*Save as Party_Unity_Correlation.gph
gr combine Party_Unity_Correlation.gph BipartisanScore_.gph, col(2) iscale(0.8)

*Table 6: Models 3 and 4
*NOTE: Table 6 includes congressional term and annual data. The two were combined in the manuscript file itself. 
reg lugar partisan_total partymeddist2 districtpartisan party_leader republican terms_served age rep94 dem06 dem08 rep10 senate cong115 if omit==0, vce(robust)
reg lugar weighted_partisan partymeddist2 districtpartisan party_leader republican terms_served age rep94 dem06 dem08 rep10 senate cong115 if omit==0, vce(robust)

*use MeasuringCongressionalPartisanship_LSQ_AnnualData.dta"
*Table 6: Models 1 and 2
reg party_unity_support partisan_total partymeddist districtpartisan party_leader republican terms_served age rep94 dem06 dem08 rep10 senate cong115 if omit==0, vce(robust)
reg party_unity_support weighted_partisan partymeddist districtpartisan party_leader republican terms_served age rep94 dem06 dem08 rep10 senate cong115 if omit==0, vce(robust)

*Table 7
reg presidential_support unfolded_partisan pres_dist dist_part pres_party presparty_leader nonpresparty_leader terms_served age senate cong115 if omit==0, vce(robust)
reg presidential_support unfolded_weighted pres_dist dist_part pres_party presparty_leader nonpresparty_leader terms_served age senate cong115 if omit==0, vce(robust)

*Table 4: Party accounts
*use partyaccounts_LSQ.dta"
*Note, values from this data are compared to same partisan_total values in the congressional term data created with the code below.
list partisan_total if congress==114
list partisan_total if congress==115
*use \MeasuringCongressionalPartisanship_LSQ_CongressTermlData.dta"
*Create party percentile ranks, by party-term
*Compare to these lists to get percentiles
egen dem_partisan_rank = rank(partisan_total) if democrat==1 & omit==0, by(congress)
egen rep_partisan_rank = rank(partisan_total) if democrat==0 & omit==0, by(congress)

gen dem_partisan_percentile = dem_partisan_rank/247 if congress==115 & omit==0 & democrat==1
replace dem_partisan_percentile = dem_partisan_rank/229 if congress==114 & omit==0 & democrat==1

gen rep_partisan_percentile = rep_partisan_rank/287 if congress==115 & omit==0 & democrat==0
replace rep_partisan_percentile = rep_partisan_rank/277 if congress==114 & omit==0 & democrat==0

sort dem_partisan_rank
list bioname partisan_total dem_partisan_percentile if democrat==1 & congress==114 & omit==0
list bioname partisan_total dem_partisan_percentile if democrat==1 & congress==115 & omit==0

sort rep_partisan_rank
list bioname partisan_total rep_partisan_percentile if democrat==0 & congress==114 & omit==0
list bioname partisan_total rep_partisan_percentile if democrat==0 & congress==115 & omit==0

*Partisan events validation check
*use partisanevents_LSQ.dta
ttest partisan_total, by(partisan_event)
